<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
</head>
<body>
<form action="#">
    <div>
        <label for="username">用户名：</label>
        <input type="text" id="username">
        <span id="usernameInfo"></span>
    </div>
    <div>
        <label for="pwd">密码：</label>
        <input type="password" id="pwd">
        <span id="pwdInfo"></span>
    </div>
    <div>
        <label for="confirm">确认密码：</label>
        <input type="password" id="confirm">
        <span id="confirmInfo"></span>
    </div>
    <div>
        <input type="submit">
    </div>
</form>
<script src="https://cdn.jsdelivr.net/npm/axios@1.1.2/dist/axios.min.js"></script>
<script>
    let username = document.getElementById("username");
    let usernameInfo = document.getElementById("usernameInfo");
    let pwd = document.getElementById("pwd");
    let pwdInfo = document.getElementById("pwdInfo");
    let confirm = document.getElementById("confirm");
    let confirmInfo = document.getElementById("confirmInfo");
    username.onblur = async function(){
        if(/^\w{5,20}$/.test(this.value)){

            // 使用ajax和后端交互
            // let xhr = new XMLHttpRequest();
            // xhr.open("get","/users?username="+this.value);
            // xhr.send();
            // xhr.onreadystatechange = function(){
            //     if(xhr.readyState == 4 && xhr.status == 200){
            //         let res = JSON.parse(xhr.responseText);
            //         if(res.code == 1){
            //             usernameInfo.innerHTML = "√";
            //         }else{
            //             usernameInfo.innerHTML = "用户名已存在";
            //         }
            //     }
            // }

            let res = await axios({
                method: "get",
                url: "/users",
                params:{
                    username: this.value
                }
            });
            let data = res.data;
            if(data.code == 1){
                usernameInfo.innerHTML = "√";
            }else{
                usernameInfo.innerHTML = "用户名已存在";
            }
        }else{
            usernameInfo.innerHTML = "用户名不符合规范";
        }
    }
    pwd.onblur = function(){
        if(/^.{6,}$/.test(this.value)){
            pwdInfo.innerHTML = "√";
        }else{
            pwdInfo.innerHTML = "密码不符合规范";
        }
    }
    confirm.onblur = function(){
        if(this.value == pwd.value){
            confirmInfo.innerHTML = "√";
        }else{
            confirmInfo.innerHTML = "两次密码不一致";
        }
    }
    document.forms[0].onsubmit = async function(event){
        event.preventDefault();
        if(usernameInfo.innerHTML == "√"
            && pwdInfo.innerHTML == "√"
            && confirmInfo.innerHTML == "√"){
            await axios({
                method: "post",
                url: "/users",
                data: {
                    username: username.value,
                    pwd: pwd.value
                }
            });
            alert("注册成功");
        }else{
            alert("验证失败");

        }
    }

</script>
</body>
</html>